package cn.danger.com.model.action;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.danger.com.db.MemoDao;
import cn.danger.com.db.MemoDaoImpl;
import cn.danger.com.db.ScheduleDao;
import cn.danger.com.db.ScheduleDaoImpl;
import cn.danger.com.model.entity.Memo;
import cn.danger.com.model.entity.MyUser;
import cn.danger.com.model.entity.Schedule;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Check;
import cn.danger.com.util.Constants;
import cn.danger.com.util.MyNetManager;

/**
 * Created by 俊 on 2016/12/28.
 */
public class LoginModelImpl implements LoginModel {

    private Context context;

    public LoginModelImpl(Context context) {
        this.context = context;
    }

    private String check(String phone, String password) {
        if (TextUtils.isEmpty(phone)) {
            return Constants.PHONE_IS_EMPTY;
        }
        if (!Check.isMobileNO(phone)) {
            return Constants.PHONE_IS_ILLEGAL;
        }
        if (TextUtils.isEmpty(password)) {
            return Constants.PASSWORD_IS_EMPTY;
        }
        return Constants.IS_RIGHT;
    }

    @Override
    public void login(String phone, String password, final OnCommonListener listener) {
        String check_result = check(phone, password);
        if (check_result.equals(Constants.IS_RIGHT)) {
            if (!new MyNetManager(context).isNetworkAvailable()) {
                listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
            } else {
                MyUser user = new MyUser();
                user.setUsername(phone);
                user.setPassword(password);
                user.login(new SaveListener<MyUser>() {
                    @Override
                    public void done(MyUser myUser, BmobException e) {
                        if (e == null) {
                            listener.onSuccess(null);
                        } else {
                            Log.i("danger", "login code-----" + e.getErrorCode());
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_LOGIN);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            }
        } else {
            listener.onError(check_result);
        }
    }

    private boolean checkUserDataIsEmpty(String phone) {
        List<Memo> memos = new MemoDaoImpl(context).getMemos(phone);
        List<Schedule> schedules = new ScheduleDaoImpl(context).getSchedules(phone);
        if (memos.isEmpty() && schedules.isEmpty()) {
            return true;
        }
        return false;
    }

    private void insertMemos(List<Memo> memos) {
        MemoDao memoDao = new MemoDaoImpl(context);
        for (Memo memo : memos) {
            memoDao.insert(memo);
        }
    }

    private void insertSchedules(List<Schedule> schedules) {
        ScheduleDao scheduleDao = new ScheduleDaoImpl(context);
        for (Schedule schedule : schedules) {
            scheduleDao.insert(schedule);
        }
    }

    @Override
    public void synchronousData(String phone, final OnCommonListener listener) {
        if (checkUserDataIsEmpty(phone)) {
            BmobQuery<Memo> query_memos = new BmobQuery<Memo>("memo");
            query_memos.addWhereEqualTo("username", phone);
            query_memos.findObjects(new FindListener<Memo>() {
                @Override
                public void done(List<Memo> memos, BmobException e) {
                    if (e == null) {
                        insertMemos(memos);
                        listener.onSuccess(Constants.SUCCESS_TO_GET_MEMO_FROM_SERVER);
                    } else {
                        if (Check.checkByCode(e.getErrorCode()) == null) {
                            listener.onError(Constants.FAIL_TO_GET_MEMO_FROM_SERVER);
                        } else {
                            listener.onError(Check.checkByCode(e.getErrorCode()));
                        }
                    }
                }
            });

            BmobQuery<Schedule> query_schedules = new BmobQuery("schedule");
            query_schedules.addWhereEqualTo("username", phone);
            query_schedules.findObjects(new FindListener<Schedule>() {
                @Override
                public void done(List<Schedule> schedules, BmobException e) {
                    if (e == null) {
                        insertSchedules(schedules);
                        listener.onSuccess(Constants.SUCCESS_TO_GET_SCHEDULE_FROM_SERVER);
                    } else {
                        if (Check.checkByCode(e.getErrorCode()) == null) {
                            listener.onError(Constants.FAIL_TO_GET_SCHEDULE_FROM_SERVER);
                        } else {
                            listener.onError(Check.checkByCode(e.getErrorCode()));
                        }
                    }
                }
            });
        } else {
            listener.onSuccess(null);
        }
    }
}
